package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;

    @Override
    public Result isFollow(Long id) {
        Long userId = UserHolder.getUser().getId();
        Follow follow = followMapper.isFollow(userId,id);
        if(follow!=null){
            return Result.ok(true);
        }else{
            return Result.ok(false);
        }

    }

    /**
     * 关注或取关
     * @param id
     * @param isFollow
     * @return
     */
    @Override
    @Transactional
    public Result follow(Long id, Boolean isFollow) {
        Long userId = UserHolder.getUser().getId();

        String key = "user:follow:" + userId;
        //查询是否关注过
        Follow result = followMapper.isFollow(userId, id);

        //关注
        if(isFollow){
            if (result == null) {
                Follow follow = new Follow();
                follow.setUserId(userId);
                follow.setFollowUserId(id);
                boolean success = save(follow);
                //写入redis
                if (success) {
                    redisTemplate.opsForSet().add(key, id);
                }
            }
        }else{
            //取关
            if(result!=null){
                int res = followMapper.deleteById(result.getId());
                if(res>0){
                    redisTemplate.opsForSet().remove(key,id);
                }
            }

        }
        return Result.ok();
    }

    /**
     * 共同关注
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result commonFollow(Long id) {
        //当前用户
        Long userId = UserHolder.getUser().getId();
        String key1 = "user:follow:" + userId;
        String key2 = "user:follow:" + id;

        //获取共同关注
        Set commonIds = redisTemplate.opsForSet().intersect(key1, key2);
        if (commonIds == null || commonIds.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }
        //转集合
        List<Long> ids = new ArrayList(commonIds);
        //查库
        List<User> userList = userService.listByIds(ids);
        //封装
        List<UserDTO> userDTOList = userList.stream()
                .map(
                user -> BeanUtil.copyProperties(user, UserDTO.class)
        )
                .collect(Collectors.toList());


        return Result.ok(userDTOList);


    }
}
